<?php
if (!defined('IS_INITPHP')) exit('Access Denied!');
/*********************************************************************************
 * InitPHP 3.3 国产PHP开发框架  - 工具库 - 树形数据获取
 *-------------------------------------------------------------------------------
 * 版权所有: CopyRight By iceman
 * 您可以自由使用该源码，但是在使用过程中，请保留作者信息。尊重他人劳动成果就是尊重自己
 *-------------------------------------------------------------------------------
 * $Author:iceman
 * $Dtime:2014-1-7
***********************************************************************************/
class treeInit {
	
	private $_parent_pk = 'parent_id'; //父字段名称
	private $_child_pk = ''; //子字段名称
	
	private $_all_data = array(); //所有数据
	
	private $_child_info = array(); //信息
	private $_parent_child = array(); //关系
	
	private $_tree = array(); //最终结构之树形
	private $_list = array(); //最终结果之列表
	
	private $_prefix_name = 'tree_prefix'; //前缀名称
	private $_sub_name = 'subs'; //子分支名称
	
	/**
	 *	设置数据
	 */
	public function set_data($data = array(), $child_pk = '', $parent_pk = 'parent_id') {
		if (empty($data) || empty($child_pk) || empty($parent_pk))	return FALSE;

		$this->_all_data = $data;

		$this->_parent_pk = $parent_pk;
		$this->_child_pk = $child_pk;
		
		$this->_child_info = array();
		$this->_parent_child = array();
		
		$this->_tree = array();
		$this->_list = array();

		return $this->_deal_data();
	}
	
	/**
	 * 处理数据
	 */
	private function _deal_data()
	{
		if (empty($this->_all_data) || empty($this->_parent_pk) || empty($this->_child_pk))	return FALSE;
		
		foreach ($this->_all_data as $v)
		{
			$pid = $v[$this->_parent_pk];
			$cid = $v[$this->_child_pk];
			
			$this->_child_info[$cid] = $v;
			$this->_parent_child[$pid][] = $cid;
		}
		
		return TRUE;
	}
	
	/**
	 * 获取树形结构
	 */
	public function get_tree($pid = 0, $include_self = FALSE)
	{
		if (empty($this->_parent_child[$pid]))	return FALSE;
		
		if ($include_self && isset($this->_child_info[$pid]))
		{
			$info = $this->_child_info[$pid];
			$info[$this->_sub_name] = $this->_build_tree($pid);

			$this->_tree[$pid] = $info;
		}
		else
		{
			foreach ($this->_parent_child[$pid] as $v)
			{
				$info = $this->_child_info[$v];
				$info[$this->_sub_name] = $this->_build_tree($v);
				
				$this->_tree[$v] = $info;
			}
		}
		
		return $this->_tree;
	}
	/**
	 * 构建树形结构
	 */
	private function _build_tree($pid = 0)
	{
		if (empty($this->_parent_child[$pid]))
		{
			return array();
		}
		
		$res = array();
		foreach ($this->_parent_child[$pid] as $v)
		{
			$info = $this->_child_info[$v];
			$info[$this->_sub_name] = $this->_build_tree($v);
			
			$res[$v] = $info;
		}
		
		return $res;
	}
	
	/**
	 * 获取树形数据的list格式
	 */
	public function get_list($pid = 0, $include_self = FALSE, $split = '--')
	{
		if (empty($this->_parent_child[$pid]))	return FALSE;
		
		$depth = 0;
		
		if ($include_self && isset($this->_child_info[$pid]))
		{
			$info = $this->_child_info[$pid];
			$info[$this->_prefix_name] = str_repeat($split, $depth);
			
			$this->_list[$pid] = $info;
			
			$depth ++;
		}
		
		$this->_build_list($pid, $depth, $split);
		
		return $this->_list;
	}
	/**
	 * 构建树形数据的list格式
	 */
	private function _build_list($pid = 0, $depth = 0, $split = '--')
	{
		if (empty($this->_parent_child[$pid]))	return FALSE;
		
		foreach ($this->_parent_child[$pid] as $v)
		{
			$info = $this->_child_info[$v];
			$info[$this->_prefix_name] = str_repeat($split, $depth);
			
			$this->_list[$v]  = $info;
			
			$this->_build_list($v, $depth+1, $split);
		}
		
		return TRUE;
	}
}